package com.sky.service.impl;

import com.sky.service.CommonService;
import com.sky.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Service
@Slf4j
public class CommonServiceImpl implements CommonService {
    @Autowired
    private AliOssUtil aliOssUtil;

    @Override
    public String upload(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        log.info("文件上传开始:{}", originalFilename);
        // 获取文件后缀
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        //构建新的文件名
        String objectName = UUID.randomUUID().toString() + extension;

        try {
            String url = aliOssUtil.upload(file.getBytes(),objectName);
            log.info("文件上传成功:{}", url);
            return url;
        } catch (IOException e) {
            log.error("文件上传失败", e);
            return null;
        }
    }
}
